package com.ec.system.modules.sys.controller;

import com.ec.core.common.core.api.ResponseMsg;
import com.ec.core.common.core.api.Result;
import com.ec.core.common.core.base.BasePageVO;
import com.ec.core.log.annotations.OperLog;
import com.ec.core.mybatis.groups.AddGroup;
import com.ec.core.mybatis.groups.DeleteGroup;
import com.ec.core.mybatis.groups.OtherGroup;
import com.ec.core.mybatis.groups.UpdateGroup;
import com.ec.core.secure.annotations.PreAuth;
import com.ec.system.modules.sys.dto.QueryTenantDTO;
import com.ec.system.modules.sys.dto.SysTenantDTO;
import com.ec.system.modules.sys.service.ISysTenantService;
import com.ec.system.modules.sys.vo.InitTenantAccountVO;
import com.ec.system.modules.sys.vo.SysTenantVO;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 租户接口
 * @author: Eric Lee
 * @time: 2021/9/22 11:30
 */
@RestController
@RequestMapping("/tenant")
public class SysTenantController {
    @Resource
    private ISysTenantService sysTenantService;

    /**
     * 租户下拉选择列表
     *
     * @return
     */
    @GetMapping("/dropDownTenants")
    public Result<List<SysTenantVO>> dropDownTenants() {
        return ResponseMsg.success(sysTenantService.dropDownTenants());
    }

    /**
     * 租户分页列表
     *
     * @param listTenantDTO
     * @return
     */
    @GetMapping("/list")
    @PreAuth("sys_tenant_view")
    public Result<BasePageVO<SysTenantVO>> listTenant(QueryTenantDTO listTenantDTO) {
        return ResponseMsg.success(sysTenantService.listTenant(listTenantDTO));
    }


    /**
     * 添加租户
     *
     * @param tenantDTO
     * @return
     */
    @PostMapping
    @PreAuth("sys_tenant_add")
    @OperLog(desc = "新增租户")
    public Result<InitTenantAccountVO> addTenant(@Validated(AddGroup.class) @RequestBody SysTenantDTO tenantDTO) {
        return ResponseMsg.success(sysTenantService.addTenant(tenantDTO));
    }

    /**
     * 修改租户
     *
     * @param tenantDTO
     * @return
     */
    @PutMapping
    @PreAuth("sys_tenant_edit")
    @OperLog(desc = "修改租户")
    public Result<Void> updateTenant(@Validated(UpdateGroup.class) @RequestBody SysTenantDTO tenantDTO) {
        return ResponseMsg.status(sysTenantService.updateTenant(tenantDTO));
    }

    /**
     * 删除租户
     *
     * @param tenantId
     * @return
     */
    @DeleteMapping("/{tenantId}")
    @PreAuth("sys_tenant_delete")
    @OperLog(desc = "删除租户")
    public Result<Void> deleteTenant(@Validated(DeleteGroup.class) @PathVariable Long tenantId) {
        SysTenantDTO dto = new SysTenantDTO();
        dto.setTenantId(tenantId);
        return ResponseMsg.status(sysTenantService.deleteTenant(dto));
    }

    /**
     * 配置租户菜单权限
     *
     * @param tenantDTO
     * @return
     */
    @PutMapping("/configTenantPer")
    @PreAuth("sys_tenant_config_per")
    @OperLog(desc = "配置租户菜单权限")
    public Result<Void> configTenantPer(@Validated(OtherGroup.class) @RequestBody SysTenantDTO tenantDTO) {
        return ResponseMsg.status(sysTenantService.configTenantPer(tenantDTO));
    }


    /**
     * 改变租户状态
     *
     * @param tenantId
     * @param tenantStatus
     * @return
     */
    @PutMapping("/changeTenantStatus/{tenantId}/{tenantStatus}")
    @PreAuth("sys_tenant_edit")
    @OperLog(desc = "改变租户状态")
    public Result<Void> changeTenantStatus(@PathVariable Long tenantId,
                                           @PathVariable Integer tenantStatus) {
        return ResponseMsg.status(sysTenantService.changeTenantStatus(tenantId, tenantStatus));
    }

}
